package com.alexander.cloundexplorer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.bean.response.FileInfoResponse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 * @author Administrator
 * @date 2018/3/9
 */

public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.ViewHolder>
{
    private Context context;
    private LayoutInflater inflater;
    private List<FileInfoResponse> list;
    private int layoutId = R.layout.fileseitem_layout;
    private OnItemClickLisnter onItemClickLisnter;

    public FilesAdapter(Context context1, List<FileInfoResponse> list1)
    {
        this.context = context1;
        inflater = LayoutInflater.from(context1);
        this.list = list1;
    }

    @Override
    public int getItemCount()
    {
        return list.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = inflater.inflate(layoutId, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position)
    {
        holder.fileNameTextView.setText(list.get(position).getFileName().concat(list.get(position).getFileNameSameNo()).concat(list.get(position).getFile_postfix()));
        holder.moreImageView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(onItemClickLisnter != null)
                {
                    onItemClickLisnter.onMoreClick(position);
                }
            }
        });
        holder.itemLayout.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(onItemClickLisnter != null)
                {
                    onItemClickLisnter.onItemClick(position);
                }
            }
        });
    }

    /**
     * 设置列表项点击监听器
     * */
    public void setOnItemClickLisnter(OnItemClickLisnter onItemClickLisnter)
    {
        this.onItemClickLisnter = onItemClickLisnter;
    }

    public interface OnItemClickLisnter
    {
        /**
         * 列表项被点击的会掉
         * @param position 列表项的下标
         * */
        void onItemClick(int position);

        /**
         * "更多"的按钮被点击
         * @param position 列表项的下标
         * */
        void onMoreClick(int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.fileName_TextView)
        TextView fileNameTextView;
        @BindView(R.id.item_Layout)
        RelativeLayout itemLayout;
        @BindView(R.id.more_ImageView)
        ImageView moreImageView;


        ViewHolder(View view)
        {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
